package evocarcassonne.tileseteditor.ui.swing.components;

import evocarcassonne.core.board.Tile;
import evocarcassonne.core.board.locations.Zone;
import evocarcassonne.core.utils.Card;
import evocarcassonne.tileseteditor.managers.TileManager;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;


public class LinksTable extends JTable {

    DefaultTableModel model;

    public LinksTable() {

        model = new DefaultTableModel();
        model.addColumn("source");
        model.addColumn("dest");
        setModel(model);

    }

    public void addObject(String src, String dest) {
        model.addRow(new Object[]{src, dest});
    }

    public void clear() {
        model = new DefaultTableModel();
        model.addColumn("source");
        model.addColumn("dest");
        setModel(model);
    }

    public void refresh() {

        clear();

        Tile tile = TileManager.getTile();

        if (tile == null) return;

        for (int i = 0; i < 12; i++) {

            Zone zone = tile.getZone(i);

            if (!zone.getLinks().isEmpty()) {
                String link = "";
                for (Zone proches : zone.getLinks()) {
                    link += getCard(proches.card) + ", ";
                }
                addObject(getCard(i), link);
            }
        }
    }

    private String getCard(int n) {

        String ret = "";

        switch (n) {

            case Card.N:
                ret = "NORD";
                break;
            case Card.NNE:
                ret = "NORD-NE";
                break;
            case Card.NNW:
                ret = "NORD-NO";
                break;

            case Card.E:
                ret = "EST";
                break;
            case Card.ENE:
                ret = "EST-NE";
                break;
            case Card.ESE:
                ret = "EST-SE";
                break;

            case Card.S:
                ret = "SUD";
                break;
            case Card.SSE:
                ret = "Sud-SE";
                break;
            case Card.SSW:
                ret = "SUD-SO";
                break;

            case Card.W:
                ret = "OUEST";
                break;
            case Card.WNW:
                ret = "OUEST-NO";
                break;
            case Card.WSW:
                ret = "OUEST-SO";
                break;
        }
        return ret;
    }
}
